﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BookExchange.Utility
{
    public static class LanguageUtil
    {
        private static string aVietnamCharaters = "à á ạ ả ã ă ắ ằ ặ ẵ ẳ â ấ ầ ậ ẫ ẩ";
        private static string eVietnamCharaters = "é è ẻ ẽ ẹ ê ế ề ể ễ ệ";
        private static string iVietnamCharaters = "í ì ỉ ĩ ị";
        private static string oVietnamCharaters = "ó ò ỏ õ ọ ô ố ồ ổ ỗ ộ ơ ớ ờ ở ỡ ợ";
        private static string uVietnamCharaters = "ú ù ủ ũ ụ ư ứ ừ ử ữ ự";
        private static string yVietnamCharaters = "ý ỳ ỷ ỹ ỵ";

        public static string RemoveSignInVietNameText(string vnText)
        {
            string result = vnText.ToLower();
            // a
            for (int i = 0; i < aVietnamCharaters.Length; i = i + 2)
            {
                result = result.Replace(aVietnamCharaters[i], 'a');
            }
            // e
            for (int i = 0; i < eVietnamCharaters.Length; i = i + 2)
            {
                result = result.Replace(eVietnamCharaters[i], 'e');
            }
            // i
            for (int i = 0; i < iVietnamCharaters.Length; i = i + 2)
            {
                result = result.Replace(iVietnamCharaters[i], 'i');
            }
            // o
            for (int i = 0; i < oVietnamCharaters.Length; i = i + 2)
            {
                result = result.Replace(oVietnamCharaters[i], 'o');
            }
            // u
            for (int i = 0; i < uVietnamCharaters.Length; i = i + 2)
            {
                result = result.Replace(uVietnamCharaters[i], 'u');
            }
            // y
            for (int i = 0; i < yVietnamCharaters.Length; i = i + 2)
            {
                result = result.Replace(yVietnamCharaters[i], 'y');
            }
            // d
            result = result.Replace('đ', 'd');
            return result;
        }

        public static bool Search(string text, string query, bool removeVietnameseSign = false)
        {
            //VULD: validate input before compare string
            text = text ?? string.Empty;
            query = query ?? string.Empty;

            //Compare string
            bool result = text.IndexOf(query, StringComparison.OrdinalIgnoreCase) >= 0;
            if (removeVietnameseSign && !result)
            {
                string unSignText = RemoveSignInVietNameText(text);
                result = unSignText.IndexOf(query, StringComparison.OrdinalIgnoreCase) >= 0;
            }
            return result;
        }
    }
}